package com.bookm.bookm.service.impl;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import cn.hutool.db.handler.BeanListHandler;
import com.bookm.bookm.entity.Reader;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.ReaderService;
import com.bookm.bookm.utils.EntityUtil;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.List;

public class ReaderServiceImpl implements ReaderService {

    @Override
    public Result register(Reader reader) {
        String pwd = reader.getPwd();
        pwd = MD5.create().digestHex(pwd);
        reader.setPwd(pwd);
        // 对密码进行简单 MD5 加密
        int rst = 0;
        try {
            rst = Db.use().insert(Entity.create("reader").parseBean(reader));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(rst);
    }

    @Override
    public Result login(Reader reader) {
        Reader user = null;
        try {
            // 这里的登录默认可以使用 id学号 email邮箱 name姓名 phone手机号
            // 密码进行 md5 加密以防止信息泄露
            user = Db.use().query("select * from reader where (id=? or name=? or email=? or phone=?) and pwd=?"
            ,BeanHandler.create(Reader.class)
            ,reader.getId(), reader.getName(), reader.getEmail(), reader.getPhone()
            ,MD5.create().digestHex(reader.getPwd()));

        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(user != null){
            // 在返回用户数据时将密码设为空，降低密码泄露几率
            user.setPwd("");
            return ResultUtil.success(user);
        } else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result getUsr(String id) {
        Reader reader = null;
        try {
            // 根据用户 id 查询用户
            reader = Db.use().find(Entity.create("reader").set("id", id), BeanHandler.create(Reader.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(reader == null){
            return ResultUtil.failure();
        }
        reader.setPwd("");
        return ResultUtil.success(reader);
    }

    @Override
    public Result getUserList() {
        List<Reader> data = null;
        try {
            // 获取用户列表并转换为 List<Reader>
            data = Db.use().find(Entity.create("reader"), BeanListHandler.create(Reader.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(data == null){
            return ResultUtil.failure();
        }
        return ResultUtil.success(data);
    }

    @Override
    public Result banUser(String id, Boolean is) {
        int rst = 0;
        try {
            // is 为真时表示禁用用户 为假时表示启用用户
            if(is){
                // 修改用户的 status 属性 1 为启用 0为禁用
                rst = Db.use().update(Entity.create().set("status", 0), Entity.create("reader").set("id", id).set("status", 1));
            } else {
                rst = Db.use().update(Entity.create().set("status", 1), Entity.create("reader").set("id", id).set("status", 0));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(rst == 1){
            return ResultUtil.success();
        } else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result updateUser(Reader user) {
        int rst = 0;
        // 获取用户 id
        String user_id = user.getId();
        try {
            rst = Db.use().update(Entity.create().parseBean(user, true, true)
                    ,Entity.create("reader").set("id",user_id));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(rst == 1){
            return ResultUtil.success(rst);
        }
        return ResultUtil.failure();
    }
}
